#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2005;
const int M = 1005;

int n, m;
int a[M][N];
bool vis[N][N];
using piii = pair<int, pii>;
void select(int a[], int b[]) {
  priority_queue<piii, vector<piii>, greater<piii>> pq;
  vector<piii> res;
  pq.push(piii(a[1] + b[1], pii(1, 1)));
  while (res.size() < n) {
    piii now = pq.top();
    pq.pop();
    int x = now.second.first, y = now.second.second;
    if (vis[x][y]) continue;
    vis[x][y] = 1;
    res.push_back(now);
    pq.push(piii(a[x + 1] + b[y], pii(x + 1, y)));
    pq.push(piii(a[x] + b[y + 1], pii(x, y + 1)));
  }
  rep(i, 1, n) {
    piii now = res[i - 1];
    int x = now.second.first, y = now.second.second;
    vis[x][y] = 0;
    b[i] = now.first;
  }
}
void solve() {
  cin >> m >> n;
  rep(i, 1, m) rep(j, 1, n) cin >> a[i][j];
  rep(i, 1, m) sort(a[i] + 1, a[i] + n + 1);
  rep(i, 2, m) select(a[i - 1], a[i]);
  rep(i, 1, n) cout << a[m][i] << " \n"[i == n];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}